package com.example.order.controller;

import com.example.order.pojo.Order;
import com.example.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/index")
    public String result(){
        return "index";
    }

    @RequestMapping("/save")
    public String save(Long userId, BigDecimal amount, HttpSession session, HttpServletRequest request){
        session.setAttribute("userId",userId);
        String orderNo = orderService.save(userId, amount);
        request.setAttribute("orderNo", orderNo);
        return "pay";
    }

    @RequestMapping("/pay")
    public String pay(String orderNo){
        Boolean b = orderService.pay(orderNo);
        if(b){
            return "success";
        }
        return "expired";
    }

    @RequestMapping("/getExpired")
    @ResponseBody
    public List<Order> getExpired(HttpSession session){
        Long userId = (Long)session.getAttribute("userId");
        return orderService.findByUserIdAndStatus(userId,2);
    }

}
